﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Rubyer;
using StockUI.Model;
using StockUI.Views;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;


namespace StockUI.ViewModels
{
    /// <summary>
    /// 首页
    /// </summary>
    public partial class MainViewModel : ObservableObject
    {
        [ObservableProperty]
        private string? ip;
        /// <summary>
        /// 子项集合
        /// </summary>
        public ObservableCollection<HamburgerMenuModel> Models { get; set; }

        /// <summary>
        /// 选项集合
        /// </summary>
        public ObservableCollection<HamburgerMenuSigleModel> OptionsModels { get; set; }

        [ObservableProperty]
        private HamburgerMenuModel currentViewItem;

        /// <summary>
        /// 更新数据进度
        /// </summary>
        [ObservableProperty]
        private double sliderValue;

        /// <summary>
        /// 数据进度条是否显示
        /// </summary>
        [ObservableProperty]
        private Visibility sliderStyle;

        /// <summary>
        /// 数据更新显示时间
        /// </summary>
        [ObservableProperty]
        private Visibility dataTextStyle;

        /// <summary>
        /// 数据更新显示时间
        /// </summary>
        [ObservableProperty]
        private string dataShowText;

        /// <summary>
        /// 下一步命令
        /// </summary>
        [RelayCommand]
        private async Task Selecte(object message)
        { 
            switch (message)
            {
                case "关于":
                    var content = new About();
                    await Dialog.Show(content, title: "关于");
                    break;
                case "笔记":
                    var notetask = new NoteTask();
                    notetask.Show();
                    
                    break;
                case "刷新数据":
                    SliderStyle = Visibility.Visible;
                    DataTextStyle= Visibility.Collapsed;
                    Thread thread = new Thread(UpdateDataOperation);
                    thread.Start();
                    break;
                default:
                    break;
            }
           // Debug.WriteLine($"HamburgerMenuItem Command: {message}");
        }

        private void UpdateDataOperation()
        {
            for (int i = 1; i <= 100; i++)
            {
                // 进度条的当前值递增
                SliderValue = i;
                // 挂起一小会
                Thread.Sleep(100);
            }

            DataShowText = $"数据更新：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
            DataTextStyle = Visibility.Visible;
            SliderStyle = Visibility.Collapsed;
            SliderValue = 0;
        }
                        
        public MainViewModel()
        {
            Ip = IPHelper.GetIP();

            Models = new ObservableCollection<HamburgerMenuModel>
            {
                new HamburgerMenuModel("今日行情",new Home(),IconType.HotelFill),
                new HamburgerMenuModel("股票数据",new Gpsj(),IconType.Database2Fill),
                new HamburgerMenuModel("持股分析",new Cgfx(),IconType.AwardFill),
                new HamburgerMenuModel("历史数据",new Lssj(),IconType.StackFill),
                new HamburgerMenuModel("公告大全",new Ggdq(),IconType.Tv2Line),
            };

            OptionsModels = new ObservableCollection<HamburgerMenuSigleModel>
            {   new HamburgerMenuSigleModel("笔记",IconType.StackLine),
                new HamburgerMenuSigleModel("退出",IconType.ShutDownLine),
                new HamburgerMenuSigleModel("更多",IconType.More2Line)
                {
                    Childs = new ObservableCollection<HamburgerMenuSigleModel>
                    {
                        new HamburgerMenuSigleModel("刷新数据",IconType.Database2Fill),
                        new HamburgerMenuSigleModel("关于",IconType.InformationFill),
                    }
                },
            };

            CurrentViewItem = Models.First();

            SliderStyle = Visibility.Collapsed;
            SliderValue = 0;
            DataTextStyle = Visibility.Visible;
            DataShowText = $"数据更新：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
        }
    }
 
}
